package org.ithasu.uBroadcast.test;

import org.ithasu.uBroadcast.UMessage;
import org.ithasu.uBroadcast.connectors.UDPConnector;

public class UMessager {

	static String MAP = "0123456789ABCDEF";

	public static int getCode(char c) {
		return MAP.indexOf(c);
	}

	public static void main(String[] args) {
		if (args.length == 0) {
			System.err.println("usage: java -jar umsg.jar <message>");
			return;
		}
		try {
			UMessage m = new UMessage();
			m.setType((byte) 0xb1);

			String code = args[0].toUpperCase();
			for (int i = 0; i < code.length() - 1; i += 2) {
				int value = 16 * getCode(code.charAt(i))
						+ getCode(code.charAt(i + 1));
				m.add((byte) (value & 0x00ff));
			}

			UDPConnector conn = new UDPConnector("umsger".getBytes(),
					UDPConnector.DEFAULT_PORT, 0);
			conn.send(m);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
